package com.power.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.power.mapper.TActivityRemarkMapper;
import com.power.model.TActivityRemark;
import com.power.model.TUser;
import com.power.query.ActivityRemarkQuery;
import com.power.service.ActivityRemarkService;
import com.power.util.LoginInfoUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {
    @Resource
    private TActivityRemarkMapper tActivityRemarkMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);
        TUser tUser = LoginInfoUtil.getUserFromToken(activityRemarkQuery.getToken());
        tActivityRemark.setCreateBy(tUser.getId());
        tActivityRemark.setCreateTime(new Date());
        tActivityRemark.setDeleted(0);
        return tActivityRemarkMapper.insertSelective(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> selectActivityRemarkByPage(Integer currentPage, Integer pageSize, ActivityRemarkQuery activityRemarkQuery) {
        PageHelper.startPage(currentPage, pageSize);
        List<TActivityRemark> activityList = tActivityRemarkMapper.selectActivityRemarkByPage(activityRemarkQuery);
        PageInfo<TActivityRemark> pageInfo = new PageInfo<>(activityList);
        return pageInfo;
    }

    @Override
    public TActivityRemark selectActivityRemarkDetailById(Integer id) {
        return tActivityRemarkMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateActivityDemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(tActivityRemark, activityRemarkQuery);
        TUser tUser = LoginInfoUtil.getUserFromToken(activityRemarkQuery.getToken());
        tActivityRemark.setEditBy(tUser.getId());
        tActivityRemark.setEditTime(new Date());
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteActivityRemarkById(Integer id) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        tActivityRemark.setId(id);
        tActivityRemark.setDeleted(1);
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }
}
